<template>
  <div class="login-container">
    <h2 class="login-title">用户登录</h2>

    <div class="form-item">
      <label for="account">账号</label>
      <div class="input-wrapper">
        <div class="input-prefix-icon">
          <van-icon name="contact" />
        </div>
        <input
          id="account"
          v-model="formData.account"
          type="text"
          placeholder="请输入账号"
        />
      </div>
    </div>

    <div class="form-item">
      <label for="password">密码</label>
      <div class="input-wrapper">
        <div class="input-prefix-icon">
          <van-icon name="lock" />
        </div>
        <input
          id="password"
          v-model="formData.password"
          :type="showPassword ? 'text' : 'password'"
          placeholder="请输入密码"
        />
        <div class="password-icon" @click="togglePasswordVisibility">
          <img
            :src="showPassword ? eyeFillIcon : eyeSlashFillIcon"
            alt="切换密码可见性"
          />
        </div>
      </div>
    </div>

    <div class="remember-me">
      <label class="checkbox-container">
        <input type="checkbox" v-model="rememberMe" />
        <span class="checkmark"></span>
        记住我
      </label>
    </div>

    <van-button
      type="primary"
      :loading="loading"
      loading-text="登录中..."
      round
      block
      @click="handleLogin"
    >
      <template #icon>
        <img :src="loginIcon" alt="登录" class="login-icon" />
      </template>
      登录
    </van-button>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import eyeFillIcon from "@/assets/eye-fill.png";
import eyeSlashFillIcon from "@/assets/eye-slash-fill.png";
import loginIcon from "@/assets/login.png";
import { Icon as VanIcon } from "vant";
import store from "@/store";
import { useRouter, useRoute } from "vue-router";

const router = useRouter();
const route = useRoute();
const redirect = ref(route.query?.redirect || "/my");
const formData = reactive({
  account: "",
  password: "",
});

const rememberMe = ref(false);
const loading = ref(false);
const showPassword = ref(false);

onMounted(() => {
  const userCredentials = localStorage.getItem("userCredentials");
  if (userCredentials) {
    const credentials = JSON.parse(userCredentials);
    formData.account = credentials.account;
    formData.password = credentials.password;
    rememberMe.value = true;
  }
});

const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value;
};

const handleLogin = async () => {
  try {
    loading.value = true;
    await store.dispatch("user/login", formData);

    if (rememberMe.value) {
      localStorage.setItem(
        "userCredentials",
        JSON.stringify({
          account: formData.account,
          password: formData.password,
        })
      );
    } else {
      localStorage.removeItem("userCredentials");
    }

    router.push({ path: redirect.value || "/my" });
  } catch (error) {
    console.error(error);
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  padding: 20px;
  max-width: 100%;
  margin: 0 auto;
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  font-size: 24px;
  color: #333;
}

.form-item {
  margin-bottom: 20px;
}

.form-item label {
  display: block;
  margin-bottom: 8px;
  font-size: 16px;
  color: #333;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-prefix-icon {
  position: absolute;
  left: 10px;
  z-index: 1;
  color: #909399;
}

input {
  width: 100%;
  height: 44px;
  padding: 0 15px 0 35px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 16px;
  color: #333;
  background-color: #fff;
  outline: none;
  transition: border-color 0.2s;
}

input:focus {
  border-color: #409eff;
}

.password-icon {
  position: absolute;
  right: 15px;
  cursor: pointer;
  color: #909399;
  font-size: 18px;
  display: flex;
  align-items: center;
}

.password-icon img {
  width: 20px;
  height: 20px;
}

.remember-me {
  margin-bottom: 25px;
}

.checkbox-container {
  display: flex;
  align-items: center;
  position: relative;
  padding-left: 30px;
  cursor: pointer;
  font-size: 16px;
  user-select: none;
}

.checkbox-container input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

.checkmark {
  position: absolute;
  left: 0;
  height: 20px;
  width: 20px;
  background-color: #fff;
  border: 1px solid #dcdfe6;
  border-radius: 2px;
}

.checkbox-container:hover input ~ .checkmark {
  background-color: #f2f6fc;
}

.checkbox-container input:checked ~ .checkmark {
  background-color: #409eff;
  border-color: #409eff;
}

.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

.checkbox-container input:checked ~ .checkmark:after {
  display: block;
}

.checkbox-container .checkmark:after {
  left: 7px;
  top: 3px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.login-button:hover {
  background-color: #66b1ff;
}

.login-button:active {
  background-color: #3a8ee6;
}

.login-icon {
  width: 24px;
  height: 24px;
  margin-right: 8px;
  vertical-align: middle;
}
</style>
